package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import model.Board;

public class BoardDao {

	public BoardDao() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	String url = "jdbc:mysql://localhost:3306/QUESTUDY";
	String user = "root";
	//String pass = "1234";

	String pass = "webclass";


	// 특정 study내의 board 리스트 불러오기
	public ArrayList<Board> getAllBoardList(int studyId) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<Board> boardList = new ArrayList<Board>();
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("select boardId, sid, board.title, memid, board.createdTime from board,user,study where studyid=sid and board.uid=userid and sid=? order by boardId desc");
			pstmt.setInt(1, studyId);
			rs = pstmt.executeQuery();
			while (rs.next()) {

				int boardId = rs.getInt("boardId");
				int sid = rs.getInt("sid");
				String title = rs.getString("title").trim();
				String memid = rs.getString("memid");
				Timestamp createdTime = rs.getTimestamp("createdTime");
				boardList
						.add(new Board(boardId, sid, title, memid, createdTime));
			}

			return boardList;

		} catch (Exception ex) {
			ex.printStackTrace();
			return boardList;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException ex) {
				}
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// Board 내용 불러오기
	public Board getBoardInfo(int bid) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Board board = null;
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("select boardId, sid, title, content, memid, createdTime, updatedTime from board,user where uid=userid and boardId=?");
			pstmt.setInt(1, bid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				int boardId = rs.getInt("boardId");
				int sid = rs.getInt("sid");
				String title = rs.getString("title").trim();
				String content = rs.getString("content").trim();
				String memid = rs.getString("memid");
				Timestamp createdTime = rs.getTimestamp("createdTime");
				Timestamp updatedTime = rs.getTimestamp("updatedTime");
				board = new Board(boardId, sid, title, content, memid,
						createdTime, updatedTime);
			}

			return board;

		} catch (Exception ex) {
			ex.printStackTrace();
			return board;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException ex) {
				}
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// board 작성
	public void insertBoardInfo(Board newBoard) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("insert into board(sid, title, content, uid, createdTime) values(?,?,?,?,sysdate())");
			pstmt.setInt(1, newBoard.getSid());
			pstmt.setString(2, newBoard.getTitle());
			pstmt.setString(3, newBoard.getContent());
			pstmt.setInt(4, newBoard.getUid());

			pstmt.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();

		} finally {
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// board 수정
	public void updateBoardInfo(Board updateBoard) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("update board set title=?, content=?, updatedTime=sysdate() where boardId=?");
			pstmt.setString(1, updateBoard.getTitle());
			pstmt.setString(2, updateBoard.getContent());
			pstmt.setInt(3, updateBoard.getBoardId());

			pstmt.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();

		} finally {
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

}